I imported an Eclipse project into Android Studio and now I'm trying to revive the proguard part. I have the following in build.gradle:
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
and now the questions are:
where and how do I add the file proguard-android.txt and proguard-rules.txt?
The syntax/format of this file in ADT is the same as it was in Eclipse?
I now noticed that I also have cannot resolve symbol 'getDefaultProguardFile'
Here you can find some more information on what getDefaultProguardFile() does. In essence, Android provides two Proguard files for you to use, one with optimizations, one without.
As for your custom proguard-rules.txt file, put it (if it isn't already there) in the same place where your build.gradle file of your App is located. See the SeriesGuide repository for an example.
Edit
Your directory structure (as can be seen in the SeriesGuide repo linked above) should be something like this (directories like src/ have been omitted):
.
├── app
│ ├── build.gradle # this is probably where you defined android { . . . }
│ │ # and thus `proguardFiles`
│ └── proguard-project.txt
└── build.gradle # setting up buildscript, maven repositories etc
Related
I'm looking to update Gradle from v4 to v5 but I'm getting the below warning on v4:
Gradle now uses separate output directories for each JVM language, but
this build assumes a single directory for all classes from a source
set. This behaviour has been deprecated and is scheduled to be removed
in Gradle 5.0.
So I believe I need to get rid of this warning before I can update to v5. Problem is I'm not really sure what it's asking me to do. How do I cahnge the build so that it doesn't assume a single directroy for all classes from a source set?
I tried adding the below to the build.gradle but I'm still getting the warning:
sourceSets {
main {
// Compiled Java classes should use this directory
java.outputDir = new File(buildDir, "classes/java/main")
}
}
You don't need to add anything to the build script.You just need to organize your project like this:
├── build.gradle
├── settings.gradle
└── src
└── main
├── java
│ └── HelloWorld.java
└── kotlin
└── Utils.kt
That deprecation message is shown when you use sourceSet.output.classesDir, which returns a File.
This has been replaced in Gradle 4.x, and removed in Gradle 5.x, by sourceSet.output.classesDirs (note the s at the end) which returns a FileCollection.
So you need to figure out where you use that in your buildscript and if not which plugin does. Note that the new method is already available in Gradle 4.x and so you should be able to upgrade your code or plugin version to make the deprecation message go away.
Good hunting!
There is a block code in build.gradle file:
Properties props = new Properties()
props.load(new FileInputStream("sampleapp.properties"))
buildConfigField "String", "apiKey", props.getProperty("apiKey")
buildConfigField "String", "apiSecret", props.getProperty("apiSecret")
buildConfigField "String", "defaultLogin", props.getProperty("defaultLogin")
buildConfigField "String", "defaultPassword", props.getProperty("defaultPassword")
I clone dev version from github, and when I open the sampleapp in Android studio, gradle building failded because of missing sampleapp.properties file.
I already registered in Dailymotion and created a apiKey for my application.
Now my question is how to generate a sampleapp.properties file so that gradle building would be successful?
Thank you.
Use project.rootProject if you are reading the properties file in a sub-project build.gradle:
Properties props = new Properties()
props.load(project.rootProject.file('sampleapp.properties').newDataInputStream())
// your code goes here
Project structure
.
├── app
│ ├── build.gradle <-- You are reading the sampleapp.properties in this gradle build file
│ └── src
├── build.gradle
├── gradle
├── gradlew
├── settings.gradle
└── sampleapp.properties
UPDATE
Your sample.properties file should look like shown below, just with your own values:
apiKey="yourKey"
apiSecret="yourSecret"
defaultLogin="yourLogin"
defaultPassword="yourPassword"
I want to access a property file from a module root, but failed to find any suitable example to access the property file as many only tells how to access from asset in android. My app structure is as follows,
├── app
│ ├── key.properties <--It is my property file, which contains app keys,to be used in app
│ └── src <-- Here i want to access the key.properties to access some keys
├── build.gradle
├── gradle
├── gradlew
├── gradlew.bat
├── settings.gradle
└── local.properties
Mike M is correct in that you should use gitignore to hide your file from git so it won't be pushed on your repository.
To expand a bit on this, here's a solution to have gradle load your properties during the build, and make them available to your app via BuildConfig.
1/ app/build.gradle
Before the android { ... } plugin, add:
Properties props = new Properties()
try {
props.load(file('keys.properties').newDataInputStream())
} catch (Exception ex) {
throw new GradleException("Missing keys.properties file.");
}
2/ app/build.gradle
In your buildTypes config, add
buildTypes {
debug {
buildConfigField "String", "KEY_MY_PROP", "\"${props.getProperty("myPropKey")}\""
}
release {
buildConfigField "String", "KEY_MY_PROP", "\"${props.getProperty("myPropKey")}\""
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
3/ Any class in your app
Your key is accessible as BuildConfig.KEY_MY_PROP
I have two following Android Studio project, structure like this:
projectA/
├----build.gradle
├----settings.gradle
├----bluewhale/
├----krill/
projectA settings.gradle file:include 'bluewhale', 'krill'
projectB/
├----build.gradle
├----settings.gradle
├----hello/
├----krill/
projectB settings.gradle file:include 'hello', 'krill'
You can see "projectA" and "projectB" contain the same module "krill". Actually, it's a library project.
My question is: how to reuse the submodule "krill" in Gradle? I don't want to include the same copy of "krill" in every project
Looking forward to your reply! Thanks!
If you have a sub-module that is used in multiple projects, you should think about extracting it to a separate project. Then you can create a dependency out of it and include it in both projects in dependencies section.
If you only use your local machine for development, without any custom repository, the best way would probably be to use the mavenLocal() repository. You can use the maven publish plugin to publish your jar into the local maven repository. It should be as simple as adding this to the new krill:
apply plugin: 'maven'
apply plugin: 'maven-publish'
publishing {
publications {
maven(MavenPublication) {
from components.java
artifact sourceJar {
classifier "sources"
}
}
}
}
repositories {
mavenLocal()
}
You might want to set the group and artifact ID. See the documentation for more info.
You can also keep krill in one of the projects, let's say ProjectA, if it has some relation to it. Then you set up the maven publishing in the krill sub-module. You can also publish to maven local by running gradle :krill:publishToMavenLocal and then use it as dependency in ProjectB.
Another option is to save the submodule outside the projectA and projectB trees and add it using something like this:
include("krill")
project(":krill") {
projectDir = new File("$settingsDir/../krill")
}
But I can't recommend this because it's hacky and your IDE might have a problem with it too.
Last thing that might be possible is to create symlinks from a directory where your krill project is located to both ProjectA and ProjectB. But that is a really bad idea e.g. when you are using a version control.
Finally I found an article here: an-alternative-multiproject-setup-for-android-studio. It works for me perfectly!
It shows us another Way different from Google’s Gradle Plugin user guide recommends
Sample code below: (Add this script to your project settings.gradle file)
include ':krill'
project(':krill').projectDir = new File('../otherProject/krill')
Project structure below:
RootFolder/
├----projectA/
│ ├----build.gradle
│ ├----settings.gradle
│ └----bluewhale/
│
├----projectB/
│ ├----build.gradle
│ ├----settings.gradle
│ └----hello/
│
└----otherProject/
├----krill/
│ └----build.gradle
│
└----otherModule/
└----build.gradle
For more details, visit gradle official document: Multi Project Builds
I'm trying to get the Gradle Java plugin to execute jUnit tests that exist in an Android project. My solution was to create a second module that applies the Java plugin, and set the test sourceSet to the app module's src/test directory.
test-module's build.gradle:
apply plugin: 'java'
...
dependencies {
...
testCompile project(':app-module')
}
sourceSets {
test {
java.srcDirs += ["${appDir}/src/test/java"]
}
}
This works fine from the command line, but Android Studio refuses to import a project that has source sets outside the submodule. It throws the error: Can't register given path of type 'TEST' because it's out of content root.
.
├── app-module
│ ├── build.gradle
│ └── src
│ ├── main
│ └── test
├── test-module
│ └── build.gradle
├── build.gradle
└── settings.gradle
I tried configuring this from the parent build.gradle, but that didn't change anything. I can add app-module as a testCompile project dependency in test-module, but that doesn't cause test-module to add app-module's tests.
Any ideas for getting test-module to run app-module's tests without provoking Android Studio's limitation about remote source sets?