Skip to content

Commit

Permalink
Expose Android's build.gradle file (#1270)
Browse files Browse the repository at this point in the history
* Update gradle wrapper

* expose build.gradle for android projects

* update paths
  • Loading branch information
jperedadnr authored Aug 8, 2024
1 parent 1f32dc4 commit 0004727
Show file tree
Hide file tree
Showing 6 changed files with 231 additions and 151 deletions.
1 change: 1 addition & 0 deletions src/main/java/com/gluonhq/substrate/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ public enum Profile {
public static final String PLIST_FILE = "Default-Info.plist";
public static final String PARTIAL_PLIST_FILE = "Partial-Info.plist";
public static final String MANIFEST_FILE = "AndroidManifest.xml";
public static final String BUILD_FILE = "build.gradle";
public static final String ANDROID_RES_FOLDER = "res";
public static final String ANDROID_KEYSTORE = "debug.keystore";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public boolean link() throws IOException, InterruptedException {
@Override
public boolean packageApp() throws IOException, InterruptedException {
prepareAndroidProject();
prepareAndroidManifest();
prepareAndroidFiles();
prepareAndroidResources();
copyAarLibraries();
copyOtherDalvikClasses();
Expand Down Expand Up @@ -519,27 +519,38 @@ private Path prepareAndroidProject() throws IOException {
Files.deleteIfExists(Path.of(androidProject.toString(), "app", "src", "main", "java", "com", "gluonhq", "helloandroid", "NativeWebView.java"));
}
androidProject.resolve("gradlew").toFile().setExecutable(true);
Path buildPath = androidProject.resolve("app").resolve("build.gradle");
FileOps.replaceInFile(buildPath, "namespace 'com.gluonhq.helloandroid'", "namespace '" + getAndroidPackageName() + "'");
FileOps.replaceInFile(buildPath,
"// OTHER_ANDROID_DEPENDENCIES", String.join("\n ", requiredDependencies()));
return androidProject;
return androidProject;
}

/**
* If android manifest is present in src/android, it will be copied to
* If build.gradle or android manifest are present in src/android, they will be copied to
* android project.
*
* Else, default android manifest is adjusted and used in project
* Else, default build.gradle and android manifest are adjusted and used in project
* configuration.
*
* @return the path where android manifest is located
* @throws IOException
*/
private Path prepareAndroidManifest() throws IOException {
private void prepareAndroidFiles() throws IOException {
String targetOS = projectConfiguration.getTargetTriplet().getOs();
Path sourcePath = paths.getSourcePath().resolve(targetOS);

Path userBuild = sourcePath.resolve(Constants.BUILD_FILE);
Path targetBuild = getAndroidProjectPath().resolve("app").resolve(Constants.BUILD_FILE);
Path generatedBuild = paths.getGenPath().resolve(targetOS).resolve(Constants.BUILD_FILE);

if (!Files.exists(userBuild)) {
// use default build.gradle
FileOps.replaceInFile(targetBuild, "namespace 'com.gluonhq.helloandroid'", "namespace '" + getAndroidPackageName() + "'");
FileOps.replaceInFile(targetBuild,
"// OTHER_ANDROID_DEPENDENCIES", String.join("\n ", requiredDependencies()));
FileOps.copyFile(targetBuild, generatedBuild);
Logger.logInfo("Default build.gradle file generated in " + generatedBuild + ".\n" +
"Consider copying it to " + userBuild + " before performing any modification");
} else {
Files.copy(userBuild, targetBuild, StandardCopyOption.REPLACE_EXISTING);
}

Path userManifest = sourcePath.resolve(Constants.MANIFEST_FILE);
Path targetManifest = getAndroidProjectMainPath().resolve(Constants.MANIFEST_FILE);
Path generatedManifest = paths.getGenPath().resolve(targetOS).resolve(Constants.MANIFEST_FILE);
Expand Down Expand Up @@ -580,7 +591,6 @@ private Path prepareAndroidManifest() throws IOException {
}
Files.copy(userManifest, targetManifest, StandardCopyOption.REPLACE_EXISTING);
}
return targetManifest;
}

/**
Expand Down
Binary file modified src/main/resources/native/android/android_project/gradle/wrapper/gradle-wrapper.jar
100644 → 100755
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.8-bin.zip
Loading

0 comments on commit 0004727

Please sign in to comment.