2

I am preparing a Gradle build script for my Android application. Before build task starts I've to change main class name and thus change imports in each *.java file.

For example, if now my main class is com.company.myApp, so the packages in each java file are imported in following way: import com.company.myApp.pacakge.* and when I rename main class to com.company.newName I've also to change imports to: import com.comapny.newName.package.*.

I am beginner with Gradle but I was trying to find information about equivalent of replace() function and I found shadow plugin but it works on JAR files. There is one more soultion which is using ReplaceTokens but it works with tokens and class name is not a token like.

Is there any way to do it with Gradle?

Blady214
  • 729
  • 6
  • 19
  • "Before build task starts I've to change main class name and thus change imports in each *.java file" -- why? – CommonsWare Nov 22 '16 at 15:47
  • @CommonsWare to build application with proper id. I think that I've found solution:http://stackoverflow.com/questions/17465353/how-to-replace-a-string-for-a-buildvariant-with-gradle-in-android-studio – Blady214 Nov 22 '16 at 15:48
  • 1
    You do not need to rename or re-package classes "to build application with proper id". You can set `applicationId` in your `build.gradle` file "to build application with proper id", based on a product flavor. – CommonsWare Nov 22 '16 at 15:52
  • You absolutely should NOT DO THAT. This will give you unpredictable build results AND will probably break once you enable proguard. – Kelevandos Nov 22 '16 at 15:53
  • Also, I think you have a misunderstanding here. As @CommonsWare said, you can set the application id in gradle, using the property. The fact that the default id is the same as the package name of your java classes is a "controlled coincidence". It does not have to be this way. – Kelevandos Nov 22 '16 at 15:57
  • Click on that package name & press *Alt + Ctrl + R* to rename – Srihari Nov 22 '16 at 15:58
  • @CommonsWare I have to do it for app branding reasons, so its not only ID reason. – Blady214 Nov 23 '16 at 09:51
  • Since nobody will see the package name of Java classes, changing the package name of Java classes has no impact on branding. – CommonsWare Nov 23 '16 at 12:10

1 Answers1

2

For others with similar issue, changing any string to other using Gradle is quite easy. I've resolved my problemu using:

copy {

    from "src_tmp"
    into "src"

    filter {
        String line -> line.replaceAll("com.company.app", "com.new.string")
    }

}
Blady214
  • 729
  • 6
  • 19