I created Android App with some gradle dependencies. Now I want to create from this project *.jar (without resources and add them separately) file or *.aar file.
I tried to create new library project (with build.xml), copied my *.java and res files and run ant jar, I'm fixing problem one by one. Is there any better solution to do this?
You need to make two modules. The first module would be the jar. This should be POJOs (Plain Old Java Object) and nothing Android. The second module would be the aar. This could depend on your first project but add on the Android specific code / resources.
Then your project (MyApp) structure would look like this
MyApp/
MyApp/build.gradle
MyApp/settings.gradle
MyApp/PoJoProject/
MyApp/PoJoProject/build.gradle
MyApp/AndroidProject/
MyApp/AndroidProject/build.gradle
Then your settings.gradle file would look something like this:
include ':PoJoProject', ':AndroidProject'
Now in the modules
In MyApp/PoJoProject/build.gradle you will want to apply the java plugin. This module will build to the desired jar format that can be ran anywhere on the normal JVM.
plugins {
id 'java'
}
version '1.00'
group 'com.example.multimodule.gradle'
repositories {
jcenter()
}
dependencies {
compile 'com.google.code.gson:gson:2.5'
testCompile 'junit:junit:4.12'
}
compileJava {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
In the MyApp/AndroidProject/build.gradle you want to apply the android plugin. This module will build to the desired aar format and can only be used as an Android dependency.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.0.0-alpha3'
}
}
apply plugin: 'com.android.application'
// version could be different from app version if needed
// `version` & `group` could also be in the top level build.gradle
version '1.00'
group 'com.example.multimodule.gradle'
repositories {
jcenter()
}
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "com.example.multiproject.gradle.android"
minSdkVersion 19
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
testOptions {
unitTests.returnDefaultValues = true
}
}
dependencies {
// let the android `aar` project use code from the `jar` project
compile project(':PoJoProject')
testCompile 'junit:junit:4.12'
}
Related
I created an app which uses native codes, when I compile it Android Studio in debug type app works perfectly but when I generate a signed aab file, the jniLibs files get compressed and it breaks functionality in the app. I would like to know if I can exclude the lib files from being compressed.
This is my project build.gradle
buildscript {
repositories {
jcenter()
maven {
url 'https://maven.google.com/'
name 'Google'
}
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:4.0.1'
classpath 'com.google.gms:google-services:4.3.3'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
mavenCentral()
maven { url "https://jitpack.io" }
maven { url "https://dl.bintray.com/yahoo/maven/" }
//maven { url "https://repo1.maven.org/maven2/" }
maven { url "https://repo.spring.io/plugins-release/" }
maven {
url 'https://maven.google.com/'
name 'Google'
}
}
}
This is my app build.gradle
apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'
android {
compileSdkVersion 32
buildToolsVersion "30.0.2"
defaultConfig {
applicationId "com.xxxxx.xxxxxxx"
minSdkVersion 21
targetSdkVersion 32
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation files('libs/achartengine-1.2.0.jar')
api 'com.android.support:cardview-v7:25.+'
api 'com.android.support:appcompat-v7:25.+'
api 'com.android.support:preference-v7:25.+'
api 'com.android.support:design:25.+'
These are the lib files when the app is compiled in debug type:
Original lib files
Lib files when I generate signed aab in android studio:
aab lib files
In debug build the app works perfectly.
In signed generated aab, the libs are compressed, as seen in the second screenshot, this breaks the libpdnsd.so and libtun2socks.so.
This is the result
If there is a way to maintain the original libs when generating aab please let me know.
Any solution is welcomed.
You just not linked the lib folder to your app App (build.gradle). And also you have to add this line android:extractNativeLibs="true" to your AndroidManifest.xml
You can do it following like this.
First add this to your AndroidManifest.xml
android:extractNativeLibs="true"
Link the lib folder to your App (build.gradle)
android {
sourceSets {
all {
jniLibs.srcDirs = ["lib"]
}
}
}
Sync it
I'm using Android Studio 3.0.1 and I have noticed that for one of my projects (only this one) it generates .project files and a bin directory like Eclipse does (I suppose).
The project was created in AS 3.0.1 and it was not migrated from Eclipse or anything like this. It also doesn't use any fancy Gradle plugins or dependencies - just the generated ones (AppCompat + Kotlin).
Do you have any idea why it does so?
Also, when I was trying to prepare a minimal project for reproduction, I noticed that the problem disappears just by copy-pasting the whole project directory so there's probably no point in sharing it with you.
Using two project copies (let's call them original and copy) I was able to find out that when I close Android Studio and clean both copies using git clean -xfd
copy has no issues when opened - no Eclipse-like files
original still has the issue when opened (files get generated)
diff -r before opening them (so just after git clean) gives only this:
Binary files ../PWTA_Proj2/.git/index and ./.git/index differ
both of them make Android Studio show an error like this when opened:
Unsupported Modules Detected: Compilation is not supported for following modules: PWTA_Proj2. Unfortunately you can't have non-Gradle Java modules and Android-Gradle modules in one project.
Despite of the error I can build the project normally. Also I don't have any non-Gradle modules - the project is very simple and generated by AS. The only less common thing is that the project contains a few AIDL files.
Edit #1: Gradle files:
top-level build.gradle:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.kotlin_version = '1.2.0'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
top-level settings.gradle:
include ':app'
app module build.gradle:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 27
buildToolsVersion '27.0.3'
defaultConfig {
final appId = "com.azabost.pwta.proj2"
applicationId appId
minSdkVersion 25
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
manifestPlaceholders.tspProcessName = appId + ".tsp"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation"org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
implementation 'com.android.support:appcompat-v7:27.0.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}
I'm new to Android and I can't get my first app to run. Whenever I try, it's throwing this error:
Android gradle :app:packageDebug FAILED: class org.bouncycastle.asn1.ASN1Primitive overrides final method equals.
I googled it and I found many questions there. As many said, this problem is probably due to the fact that I have multiple dependencies on BouncyCastle in my project. I tryed to remove the .gradle folder both in my project and in my local folder so I could download again the jars from the repository but nothing has changed. I also double checked that my JAVA_HOME is set and it's pointing to a 1.7 version. I tryed to gradle :app:clean and gradle :app:dependencies to see if I was missing something but still... I can't see any dependency from BouncyCastle... Can anybody help me?
Also, this problem occured without me changing any code... The app successfully worked yesterday and now is giving me this error... I don't get it. Thank you.
EDIT
gradle.build
Top-level:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.1.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
Module app:
apply plugin: 'com.android.application'
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "com.example.aurasphere.provaactionbar"
minSdkVersion 11
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.0.0'
compile 'com.google.android.gms:play-services:7.0.0'
compile project(':volley')
compile files('libs/gson-2.2.2.jar')
}
Module volley:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.14.+'
}
}
apply plugin: 'com.android.library'
android {
compileSdkVersion 19
buildToolsVersion = '21.1.0'
}
apply from: 'rules.gradle'
rules.gradle for module volley:
apply plugin: 'com.android.library'
I've solved this problem myself just by changing the JDK I was using from x32 to x64 and now is compiling correctly.
I have integrated Twitter and all is done well.
But now I need to create a reusable component.
I.e.: a library using the fabric sdk in android studio.
We can get the fabric plugin from the Twitter developer site.
By using that I'm able to add sdk to an Android Studio project, but not to a library.
How to add this sdk to an Android Studio library project?
Define the classpath in your project's build.gradle as usual.
Add the dependencies to your library module as you would suspect.
Apply the io.fabric plugin in your app module. No way around this, that's by design. Fabric needs application ID, libraries don't have one.
EDIT: Due to manifest merging you are able to specify the API key meta-data (e.g. for Crashlytics) in your library's manifest.
EDIT 2: Looks like there's a page for it. http://support.crashlytics.com/knowledgebase/articles/456258-library-subproject-in-gradle
https://docs.fabric.io/android/crashlytics/build-tools.html#set-up-a-library-subproject
For creating reusable component with fabric sdk.update your gradle to something like this
buildscript {
repositories {
maven { url 'https://maven.fabric.io/public' }
}
dependencies {
classpath 'io.fabric.tools:gradle:1.+'
}
}
apply plugin: 'com.android.library'
apply plugin: 'io.fabric'
repositories {
maven { url 'https://maven.fabric.io/public' }
}
android {
compileSdkVersion 22
buildToolsVersion "21.1.2"
defaultConfig {
minSdkVersion 14
targetSdkVersion 22
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.0.0'
compile('com.twitter.sdk.android:twitter:1.3.2#aar') {
transitive = true;
}
}
Do rebuild your project after updating gradle file.you wil get the skd access now.
I tried many options from google and stackoverflow.
Android studio 0.4.6 and aa 2.7.1.
My build.gradle looks like this
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.7.+'
}
}
apply plugin: 'android'
repositories {
mavenCentral()
}
android {
compileSdkVersion 19
buildToolsVersion "19.0.1"
defaultConfig {
minSdkVersion 8
targetSdkVersion 19
versionCode 1
versionName "1.0"
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}
ext.daggerVersion = '1.0.0';
ext.androidAnnotationsVersion = '2.7.1';
configurations {
apt
}
dependencies {
compile 'com.android.support:appcompat-v7:+'
apt "com.googlecode.androidannotations:androidannotations:${androidAnnotationsVersion}"
compile "com.googlecode.androidannotations:androidannotations-api:${androidAnnotationsVersion}"
apt "com.squareup.dagger:dagger-compiler:${daggerVersion}"
compile "com.squareup.dagger:dagger:${daggerVersion}"
}
I've checked Activities_ in AndroidManifest.xml too.
If this quesion were not banned due to other "similar" questions I would prefer answers from people who did it with exacly the same as version. I know there will be next version's and maybe new questions but this looks litle tricky imho. Even though android development with aa is very robust.
"sync project with gradle files", clean, rebuild etc brings no effect.
EDIT:
I tryied with
ext.daggerVersion = '1.2.0';
ext.androidAnnotationsVersion = '3.0.1';
but it brings no result.
After switching to:
classpath 'com.android.tools.build:gradle:0.8.+'
and
ext.daggerVersion = '1.2.0';
it builds succesfully but outputs with:
Default Activity not found
I checked Error: Default Activity Not Found with no effect.
It looks like you need to add the android-apt dependency within the BuildScript dependencies and let gradle know to use it as a plugin.
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.8.+'
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.2'
}
}
apply plugin: 'android'
apply plugin: 'android-apt'
This is my apt config section, you may need other settings but this should get you started
apt {
arguments {
resourcePackageName android.defaultConfig.packageName
androidManifestFile variant.processResources.manifestFile
}
}
Lot's more info here: https://bitbucket.org/hvisser/android-apt